import Vue from 'vue'
import Vuex from 'vuex'
import Stomp from 'stompjs';
Vue.use(Vuex)
const rabbitMQStore = {
	state: {
		conncted: '',
		client: null,
		currentSubscribe: null,
		options: {
			vhost: '/', // rabbitmq的vhost
			incoming: 8000, // 心跳包时间，（须大于0，且小于10000，因为服务器可能默认10秒没心跳就会断开）
			outgoing: 8000, // 心跳包时间，（须大于0，且小于10000，因为服务器可能默认10秒没心跳就会断开）
			account: 'device_mqtt', // rabbitmq的账户
			pwd: 'device_mqtt', // rabbitmq的密码
			server: process.env.VUE_APP_RABBITMQ_API // ws://rabbitmq的ip:rabbitmq的web-stomp的端口/ws
		},
		eventlist: [],
	},
	mutations: {
		RABBITMQ_INIT(state) {
			let that = this;

			if (state.conncted) {
				return
			}
			const mqUrl = state.options.server; // 连接地址
			const ws = new WebSocket(mqUrl); // 创建
			ws.onclose = close => {
				console.log('webSocket关闭', close); // 关闭回调
			};
			ws.onerror = error => {
				console.log('webSocket异常', error); // 异常回调
			};
			ws.onopen = success => {
				console.log('webSocket连接成功', success); // 成功回调
			};
			state.client = Stomp.over(ws);
			state.client.heartbeat.incoming = state.options.incoming;
			state.client.heartbeat.outgoing = state.options.outgoing;

			//开始连接
			state.client.connect(
				state.options.account, // 用户名
				state.options.pwd, // 密码
				() => {
					console.log('stomp 连接成功！');
					state.conncted = true;
					//心电数据
					that.commit('doSubscribe', '/exchange/device_data_fanout');
					// //位置信息
					// that.commit('doSubscribe', '/queue/device_data_location');
					// //绑定结果
					// that.commit('doSubscribe', '/queue/device_bind_result');
					// //去监听电量
					// that.commit('doSubscribe', '/queue/device_data_electric');
					//onSuccessConnectRabbitmq
					//模拟发送数据
					// setInterval(() => {
					// 	let quote = JSON.parse(
					// 		"{\"traceId\":\"F2F491AFF2364D92A142B4E828F3A89F\",\"deviceGateWayMac\":\"E9:8C:B8:9A:BD:D5\",\"heartRateStatus\":0,\"hr\":47,\"type\":\"HR\",\"patientCode\":\"456778\"}"
					// 		)
					// 	state.eventlist.push({
					// 		method: quote.type,
					// 		data: quote
					// 	})
					// }, 300)
				}, // 连接成功时回调
				(errorMsg) => {
					//onErrorConnectRabbitmq
					state.conncted = false;
					console.error(`stomp 断开连接，正在准备重新连接...`, errorMsg);
					that.commit('RABBITMQ_INIT');
				}, // 失败时回调
				state.options.vhost
			);

		},

		onSuccessConnectRabbitmq(state) {
			// 直接在连接成功的时候就订阅监听user.audit.queue队列 user.audit.queue是rabbitmq里创建的queue名称
			// this.doSubscribe('/queue/test');
		},
		onErrorConnectRabbitmq(errorMsg) {

		},
		doSubscribe(state, queueName) {
			state.currentSubscribe = state.client.subscribe(queueName, function(messages) {
				//console.log('receive:', messages);
				// console.log('message body', messages.body); // 消息内容主体 json需要自己转
				let url = window.location.href;
				var quote = JSON.parse(JSON.parse(messages.body));
				state.eventlist.push({
					method: quote.type,
					data: quote
				})
				// if (queueName == '/queue/device_bind_result') {
				// 	state.eventlist.push({
				// 		method: 'bindStatus',
				// 		data: quote
				// 	})
				// } else if (url.indexOf('dashboard') > -1) {
				// 	switch (queueName) {
				// 		case '/queue/device_data_transfer': {
				// 			state.eventlist.push({
				// 				method: quote.type,
				// 				data: quote
				// 			})
				// 			break;
				// 		}
				// 		case '/queue/device_data_location': {
				// 			state.eventlist.push({
				// 				method: 'location',
				// 				data: quote
				// 			})
				// 			break;
				// 		}
				// 		case '/queue/device_data_electric': {
				// 			state.eventlist.push({
				// 				method: 'electric',
				// 				data: quote
				// 			})
				// 			break;
				// 		}
				// 	}
				// }
				// console.log('queueName', queueName, state.eventlist)
			});
		},
		doUnSubscribe(state) {
			state.currentSubscribe.unsubscribe();
		},
		createConnection(state) {
			this.commit('RABBITMQ_INIT');

		},
		destroyConnection(state) {
			state.client.disconnect(() => {
				console.log('已关闭rabbitmq连接');
				state.conncted = false;
			});
		}

	},
	actions: {

	}
}
export default rabbitMQStore